package com.wjj.sixinone.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjj.sixinone.common.enums.ExceptionEnum;
import com.wjj.sixinone.common.enums.RedisConstantEnum;
import com.wjj.sixinone.common.enums.UserEnum;
import com.wjj.sixinone.common.exception.GlobalException;
import com.wjj.sixinone.common.pojo.dto.LoginDto;
import com.wjj.sixinone.common.pojo.dto.UserInfo;
import com.wjj.sixinone.common.pojo.entity.User;
import com.wjj.sixinone.common.util.AuthContextUtil;
import com.wjj.sixinone.common.util.JwtUtil;
import com.wjj.sixinone.common.util.MD5Util;
import com.wjj.sixinone.common.util.Result;
import com.wjj.sixinone.mapper.UserMapper;
import com.wjj.sixinone.service.OpenService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
public class OpenServiceImpl extends ServiceImpl<UserMapper, User> implements OpenService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Resource
    private UserMapper userMapper;
    @Resource
    private JwtUtil jwtUtil;
    @Override
    public Result Login(LoginDto loginDto) {
        String code = loginDto.getCode();
        String codeKey= RedisConstantEnum.USER_VERIFY_CODE.getKey() + loginDto.getCodeKey();

        String codeRedis = redisTemplate.opsForValue().get(codeKey);
        if (codeRedis == null) {
            throw new GlobalException(ExceptionEnum.CODE_EXPRESS);
        }

        if (!codeRedis.equals(code)) {
            throw new GlobalException(ExceptionEnum.CODE_ERROR);
        }
        redisTemplate.delete(RedisConstantEnum.USER_VERIFY_CODE.getKey() + loginDto.getCodeKey());
        String username = loginDto.getUsername();
        LambdaQueryWrapper<User> select = new LambdaQueryWrapper<User>().select
                (User::getId, User::getUsername, User::getPassword,User::getFaceRegister, User::getUserRole,User::getAvatar,User::getEmail).eq(User::getUsername, username);
        User user = userMapper.selectOne(select);
        if (user == null){
            throw new GlobalException(ExceptionEnum.INVALID_USERNAME_PASSWORD);
        }
        boolean flag = MD5Util.getSaltverifyMD5(loginDto.getPassword(), user.getPassword());
        if (!flag){
            throw new GlobalException(ExceptionEnum.INVALID_USERNAME_PASSWORD);
        }


        String token = jwtUtil.createToken(user.getId(), user.getUserRole());

        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(user, userInfo);
        String userKey=UserEnum.USER_ID.getKey()+token;

        redisTemplate.opsForValue().set(userKey, com.alibaba.fastjson.JSON.toJSONString(userInfo),7, TimeUnit.DAYS);


        return Objects.requireNonNull(Result.ok().put("token", token)).put("data", userInfo);
    }


}
